//
//  ActionSheetView.m
//  ZPActionSheetDemo
//  联系开发者:QQ1397819210
//  Created by 郑鹏 on 15/6/4.
//  Modified by yufeng.wu 2015
//  Copyright (c) 2015年 pzheng. All rights reserved.
//

#import "ZPActionSheetView.h"
#import "WYFSharePanelAdapter.h"

#define ACTIONSHEET_BACKGROUNDCOLOR [UIColor colorWithRed:1.00f green:1.00f blue:1.00f alpha:1]
#define WINDOW_COLOR [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]
#define ANIMATE_DURATION 0.25f

#define ActionSheetW [[UIScreen mainScreen] bounds].size.width
#define ActionSheetH [[UIScreen mainScreen] bounds].size.height

static const int LAYOUT_MARGIN_HORIZONTAL = 7;
static const int LAYOUT_MARGIN_VERTICAL = 14;
static const int ROW_ITEM_HEIGHT = 72;
static const int ROW_LAYOUT_HEIGHT = ROW_ITEM_HEIGHT + 2 * LAYOUT_MARGIN_VERTICAL;
static const int COLUMN_COUNT = 4;

@interface ZPActionSheetView ()

@property (nonatomic, strong) WYFSharePanelAdapter *sharePanelAdapter;
@property (nonatomic, assign) CGFloat LXActionSheetHeight;
@property (nonatomic, strong) UIView *backGroundView;
@property (nonatomic, strong) UIView *topsheetView;

@end

@implementation ZPActionSheetView

- (id)initWithSharePanelAdapter:(WYFSharePanelAdapter *)sharePanelAdapter {
    self = [super init];
    if (self) {
        self.sharePanelAdapter = sharePanelAdapter;

        self.frame = CGRectMake(0, 0, ActionSheetW, ActionSheetH);
        self.backgroundColor = WINDOW_COLOR;
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];

        [self loadUiConfig];
    }
    return self;
}

- (void)setOtherBtnColor:(UIColor *)otherBtnColor {
    for (id res in _backGroundView.subviews) {
        if ([res isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)res;
            [button setTitleColor:otherBtnColor forState:UIControlStateNormal];
        }
    }
}

- (void)setOtherBtnFont:(NSInteger)otherBtnFont {
    for (id res in _backGroundView.subviews) {
        if ([res isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)res;
            button.titleLabel.font = [UIFont systemFontOfSize:otherBtnFont];
        }
    }
}

- (void)setDuration:(CGFloat)duration {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:duration];
}

- (void)loadUiConfig {
    [self addSubview:self.backGroundView];
    [_backGroundView addSubview:self.topsheetView];

    _LXActionSheetHeight = 0;

    for (NSInteger i = 0; i < [self.sharePanelAdapter getCount]; i++) {
        ZPActionButton *button = [ZPActionButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(_backGroundView.bounds.size.width / COLUMN_COUNT * (i % COLUMN_COUNT),
                                  _LXActionSheetHeight + (i / COLUMN_COUNT) * ROW_LAYOUT_HEIGHT,
                                  _backGroundView.bounds.size.width / COLUMN_COUNT, ROW_ITEM_HEIGHT);

        button.titleLabel.numberOfLines = 2;
        [button setTitle:[self.sharePanelAdapter getLabelAtIndex:i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[self.sharePanelAdapter getIconNameAtIndex:i]] forState:UIControlStateNormal];
        button.tag = i;
        [button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.topsheetView addSubview:button];
    }

    [UIView animateWithDuration:ANIMATE_DURATION
                     animations:^{
                         _backGroundView.frame = CGRectMake(0, ActionSheetH - CGRectGetHeight(_backGroundView.frame),
                                                            ActionSheetW, CGRectGetHeight(_backGroundView.frame));
                     }];
}

- (void)BtnClick:(UIButton *)btn {
    [self tappedCancel];
    _btnClick(btn.tag);
}

- (void)tappedCancel {
    [UIView animateWithDuration:ANIMATE_DURATION
        animations:^{
            [self.backGroundView setFrame:CGRectMake(0, ActionSheetH, ActionSheetW, 0)];
            self.alpha = 0;
        }
        completion:^(BOOL finished) {
            if (finished) {
                [self removeFromSuperview];
            }
        }];
}

- (void)noTap {
}

#pragma mark-------- getter
- (UIView *)backGroundView {
    NSUInteger count = [self.sharePanelAdapter getCount];
    if (_backGroundView == nil) {
        _backGroundView = [[UIView alloc] init];

        NSInteger index = (count - 1) / COLUMN_COUNT + 1;
        _backGroundView.frame =
            CGRectMake(0, ActionSheetH, ActionSheetW, ROW_LAYOUT_HEIGHT * index + 2 * LAYOUT_MARGIN_HORIZONTAL);

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noTap)];
        [_backGroundView addGestureRecognizer:tapGesture];
    }
    _backGroundView.backgroundColor = [UIColor whiteColor];

    return _backGroundView;
}

- (UIView *)topsheetView {
    if (_topsheetView == nil) {
        _topsheetView =
            [[UIView alloc] initWithFrame:CGRectMake(0, LAYOUT_MARGIN_VERTICAL, CGRectGetWidth(_backGroundView.frame),
                                                     CGRectGetHeight(_backGroundView.frame))];
        _topsheetView.backgroundColor = [UIColor whiteColor];
        _topsheetView.clipsToBounds = NO;
        _topsheetView.clipsToBounds = YES;
    }
    return _topsheetView;
}

@end
